package com.haonan.springboot_exercise.controller;

import com.auth0.jwt.interfaces.Claim;
import com.haonan.springboot_exercise.bean.User;
import com.haonan.springboot_exercise.bean.pojo.*;
import com.haonan.springboot_exercise.dao.AdminDao;
import com.haonan.springboot_exercise.service.MailService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author haonan
 * @date 2021/4/15 20:50
 **/
@RestController
@CrossOrigin(origins = "http://localhost:9527",maxAge = 1800)//解决跨域问题
public class MailController {
    @Autowired
    MailService mailService;
    @Autowired
    AdminDao adminDao;

    /**
     * 发邮件，接受的实体是 ReceiveMail
     * @return
     * @throws Exception
     */
    @RequestMapping("/inbox/sendmail")
    public Result sendMail(@RequestBody MyMailMessage myMailMessage) throws Exception {
        if(mailService.getOutbox(myMailMessage.getFrom()).size() >= adminDao.getBoxSize("smtp")){
            return Result.fail("发件箱已满，无法继续发邮件\n"+"发件箱大小为:"+adminDao.getBoxSize("smtp")+"\n建议删除一部分已发送邮件");
        }
        return Result.msgResult2Result(mailService.sendMyMail(myMailMessage));
    }

    @RequestMapping("/inbox/list")
    public Result getInbox(@RequestParam("user_name") String user_name) throws Exception {
        //封装邮件，返回result
        List<ReadableMail> mails= mailService.getInbox(user_name);
        return Result.succ("当前共有"+mails.size()+"封邮件",mails);
    }

    @RequestMapping("/inbox/deleteMail")
    public Result deleteMail(@RequestBody List<Integer> emailIdList){
        return mailService.deleteEmail(emailIdList);
    }

    @RequestMapping("/outbox/list")
    public Result getOutbox(@Param("user_name")String user_name) throws Exception{
        //封装邮件，返回result
        List<ReadableMail> mails = mailService.getOutbox(user_name);
        return Result.succ("当前共有" + mails.size() + "封邮件", mails);
    }
}
